package com.wms.controller;

import com.mes.entity.User;
import com.mes.utils.GenerateCodeUtil;
import com.yy.base.page.Message;
import com.yy.base.util.StringUtil;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("buystorage")
public class BuyStorageController {
    @Autowired
    private com.wms.service.BuyStorageService buyStorageService;


    //加载对应的物料编码，及楦头系列
    @ResponseBody
    @RequestMapping(value = "selectAllList", method = RequestMethod.POST)
    public Message selectAllList(HttpServletRequest request) {
        Message msg = new Message();

        msg.setData("XuantouList",buyStorageService.getXuantouList());
        msg.setData("ProductJbList",buyStorageService.getProductJbList());
        msg.setData("ProductYdmList",buyStorageService.getProductYdmList());
        msg.setData("SizeList",buyStorageService.getSizeList());

        return msg;
    }

    //根据对应的楦头系列加载楦型
    @ResponseBody
    @RequestMapping(value = "selectProductXtList", method = RequestMethod.POST)
    public Message selectProductXtList(HttpServletRequest request) {
        Message msg = new Message();

        String xtxlId = request.getParameter("xtids").toString();   //楦头系列ID

        msg.setData("xxList",buyStorageService.getProductList(xtxlId));

        return msg;
    }


    //处理采购入库结果(楦头、压底膜)
    @ResponseBody
    @RequestMapping(value="saveProduct",method= RequestMethod.POST)
    public Message saveProduct(HttpServletRequest request){
        Message msg = new Message();

        try {

            String productId = request.getParameter("productId").toString();   //物料ID
            String productCode = request.getParameter("productCode").toString();   //楦型
            String sizeId = request.getParameter("sizeId").toString();   //尺码ID
            String sizeCode = request.getParameter("sizeCode").toString();   //尺码
            String chestr = request.getParameter("chestr").toString();   //左右脚
            String RFID = request.getParameter("RFID").toString();   //RFID规则
            int qty = 1;
            String  warehouseID ="1E0A69B1B7BB4EECB232D6989684C4D7";
            String type = "";
            String foottype = "";

            String fw = "";
            String uuid = null;

            if(chestr.equals("左脚")){
                fw = "L";
                foottype = "left";
            }else{
                fw = "R";
                foottype = "right";
            }


            //该物料是否已存在批次
            List<Map<String, Object>> batch = buyStorageService.getBatchList(sizeId,productId);




            //物料分类
            List<Map<String, Object>> wlfl = buyStorageService.getProductFlList(productId);

            String fltype = wlfl.get(0).get("FLTYPE").toString();

            String uomid = wlfl.get(0).get("UOMID").toString();
            String  WLVALUE= wlfl.get(0).get("WLVALUE").toString();


            if(fltype.equals("SC-XT")){

                type = "楦头";

                String kuanshi =  WLVALUE.substring(4,6);

                User user = UserUtil.getUser();

                String Batch_No = GenerateCodeUtil.getCode("Wms_Storage_Batch_Item", "Code");  //楦头生成对应批次

                String XTCODE = "L19"+kuanshi+"-"+sizeCode+productCode+"-"+fw+Batch_No;

                if(batch.size() != 0 ){
                    uuid = batch.get(0).get("BATCHID").toString();
                }else{
                    uuid = StringUtil.getUUID();
                    buyStorageService.inStorageBatch(uuid , sizeId, uomid,  productId, user);
                }

                buyStorageService.inStorageInOut(uuid ,warehouseID , qty,uomid,user);

                buyStorageService.inStorageItem(uuid , type,  XTCODE , user,foottype);

                msg.setData("rfidcode",XTCODE);
            }else if(fltype.equals("SC_YDM")){
                type = "压底膜";

                String kuanshi =  WLVALUE.substring(4,6);

                User user = UserUtil.getUser();

               String Batch_No = GenerateCodeUtil.getCode("Wms_Storage_Batch_Item", "Type");  //压底膜生成对应批次

                String YDMCODE = "Y19"+kuanshi+"-"+sizeCode+"-"+fw+Batch_No;

                if(batch.size() != 0 ){
                    uuid = batch.get(0).get("BATCHID").toString();
                }else{
                    uuid = StringUtil.getUUID();
                    buyStorageService.inStorageBatch(uuid , sizeId, uomid,  productId, user);
                }


                buyStorageService.inStorageInOut(uuid ,warehouseID , qty,uomid,user);

                buyStorageService.inStorageItem(uuid , type,  YDMCODE , user,foottype);

                msg.setData("rfidcode",YDMCODE);
            }else if(fltype.equals("JB")){
                type = "夹板";

                User user = UserUtil.getUser();
                if(batch.size() != 0 ){
                    uuid = batch.get(0).get("BATCHID").toString();
                }else{
                    uuid = StringUtil.getUUID();
                    buyStorageService.inStorageBatch(uuid , sizeId, uomid,  productId, user);
                }

                buyStorageService.inStorageInOut(uuid ,warehouseID , qty,uomid,user);

                List<Map<String, Object>> Rfidqty = buyStorageService.getProductJb(RFID);
                if(Rfidqty.size() == 0){
                    buyStorageService.inStorageItem(uuid , type,  RFID , user,foottype);
                }

                msg.setData("rfidcode",RFID);
            }
        }catch (Exception e){
            msg.setData("results","false");

            e.printStackTrace();
        }

        return msg;

    }
}
	
	
	
	
